<template>
  <div>
    <van-nav-bar class="van-bar" title="账号登录" left-arrow @click-left="goToMy" />
    <van-form @submit="onSubmit" class="van-form">
      <van-field
        class="van-field"
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '请填写用户名' },{ pattern: /^\w{5,}$/, message: '用户名至少包含5个字符'}]"
      />
      <van-field
        class="van-field"
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' },{ pattern: /^\w{6,}$/, message: '密码至少包含6个字符'}]"
      />
      <div>
        <van-button round block type="info" native-type="submit" class="van-button">登 录</van-button>
      </div>
    </van-form>
    <router-link class="link" to="/register">还没有账号，去注册~</router-link>
  </div>
</template>

<script>
import request from '../utils/request'
export default {
  name: 'HaokezfLogin',

  data () {
    return {
      username: '',
      password: ''
    }
  },

  mounted () {},

  methods: {
    async onSubmit (val) {
      try {
        const { body, description } = await request.post('/user/login', val)
        localStorage.setItem('MY-HK-TOKEN', body.token)
        localStorage.setItem('MY-HK-username', val.username)
        this.$store.commit('setToken', body.token)
        this.$toast.success(description)
        this.$router.push('/layout/my')
      } catch (err) {
        this.$toast.fail('账号或者密码错误')
      }
    },
    goToRegister () {
      this.$router.push('/register')
    },
    goToMy () {
      this.$router.push('/layout/my')
    }
  }
}
</script>

<style lang="less" scoped>
.van-bar {
  background-color: #21b97a;
  color: #fff;
}
.van-form {
  width: 345.2px;
  height: 206px;
  margin: 0 auto 30px;
  margin-top: 21px;
  .van-field {
    position: relative;
    height: 60px;
    padding: 2px 0;
    margin-bottom: 5px;
    font-size: 17px;
    line-height: 60px;
    // border-bottom: 1px solid #ddd;
    &::placeholder {
      color: #000;
    }
    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      height: 1px;
      background-color: #ddd;
      transform: scale(1, 0.5);
    }
  }
  .van-button {
    width: 100%;
    height: 50px;
    margin: 30px 0 0;
    line-height: 50px;
    background-color: #1cb676;
    font-size: 18px;
    border-radius: 1px;
    border: 1px solid #1cb676;
  }
}
.link {
  display: block;
  text-align: center;
  color: #666;
  font-size: 14px;
}
</style>
